// 提示信息仅在开发环境生效
import { createI18n } from "vue-i18n";
import store from "@/store";

const files = import.meta.globEager("./modules/*.js");

let messages = {};
Object.keys(files).forEach(c => {
  const module = files[c].default;
  const moduleName = c.replace(/^\.\/(.*)\/(.*)\.\w+$/, "$2");
  messages[moduleName] = module;
});

// const lang = navigator.userLanguage || navigator.language; // 初次进入，采用浏览器当前设置的语言，默认采用中文
//
// const locale = lang.indexOf("en") !== -1 ? "en" : "zh-cn";

const lang =
  store.state.app.lang || navigator.userLanguage || navigator.language; // 初次进入，采用浏览器当前设置的语言，默认采用中文
const locale = lang.indexOf("en") !== -1 ? "en" : "zh-cn";

const i18n = createI18n({
  __VUE_I18N_LEGACY_API__: false,
  __VUE_I18N_FULL_INSTALL__: false,
  globalInjection: true,
  locale: locale,
  fallbackLocale: "zh-cn",
  messages
});
document.querySelector("html").setAttribute("lang", locale);

export default i18n;
